package com.ltedu.course.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ltedu.common.contants.Constants;
import com.ltedu.common.validate.Create;
import com.ltedu.common.validate.Update;
import com.ltedu.common.vo.Result;
import com.ltedu.course.domain.Course;
import com.ltedu.course.domain.dto.CourseDO;
import com.ltedu.course.domain.vo.CourseQueryVO;
import com.ltedu.course.domain.vo.CourseVO;
import com.ltedu.course.service.CourseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author Jerry
 * @since 2020-11-26
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 添加课程
     *
     * @param courseVo
     * @return {@link Result}
     */
    @PostMapping("/add")
    public Result add(@Validated(Create.class) @RequestBody CourseVO courseVo) {
        return Result.ok(courseService.save(courseVo));
    }

    /**
     * 更新课程
     *
     * @param courseVo
     * @return {@link Result}
     */
    @PutMapping("/update")
    public Result update(@Validated(Update.class) @RequestBody CourseVO courseVo) {
        return Result.ok(courseService.save(courseVo));
    }

    /**
     * 删除课程
     *
     * @param id 课程id
     * @return {@link Result}
     */
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        // 删除视频信息 TODO
        // 删除小节信息 TODO
        // 删除章节信息 TODO
        // 删除课程信息 TODO
        return Result.ok();
    }

    /**
     * 分页查询课程列表
     *
     * @param page          页面
     * @param limit         限制
     * @param courseQueryVO 课程查询签证官
     * @return {@link Result}
     */
    @PostMapping("/list/{page}/{limit}")
    public Result list(@PathVariable Integer page,
                       @PathVariable Integer limit,
                       @RequestBody CourseQueryVO courseQueryVO) {
        Page<Course> coursePage = new Page<>(page, limit);
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(courseQueryVO.getTitle())) {
            queryWrapper.like("c.title", courseQueryVO.getTitle());
        }
        if (courseQueryVO.getState() != null) {
            queryWrapper.eq("c.state", courseQueryVO.getState());
        }
        if (courseQueryVO.getIsFree() != null) {
            queryWrapper.eq("c.is_free", courseQueryVO.getIsFree());
        }
        if (courseQueryVO.getSaleState() != null) {
            queryWrapper.eq("c.sale_state", courseQueryVO.getSaleState());
        }
        if (courseQueryVO.getBegin() != null) {
            queryWrapper.gt("c.gmt_create", courseQueryVO.getBegin());
        }
        if (courseQueryVO.getEnd() != null) {
            queryWrapper.lt("c.gmt_create", courseQueryVO.getEnd());
        }
        queryWrapper.eq("c.is_deleted", Constants.NOT_DELETED);
        queryWrapper.orderByDesc("gmtCreate");
        return Result.ok(courseService.coursePageList(coursePage, queryWrapper));
    }

    /**
     * 根据课程id获取课程信息
     *
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        return Result.ok(courseService.getCourseById(id));
    }

    /**
     * 获取课程确认信息
     *
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("/getCourseInfo/{id}")
    public Result getCourseInfo(@PathVariable Long id) {
        return Result.ok(courseService.getCourseInfoById(id));
    }

    /**
     * 提交审核
     *
     * @param courseId 进程id
     * @return {@link Result}
     */
    @PostMapping("/submit")
    public Result submit(@RequestParam Long courseId) {
        // TODO 需求：课程添加成功并提交审核，待考虑的地方
        // TODO 是否需要将审核服务拆分成一个服务？
        // TODO 提交审核方式？
        // TODO 1.调用审核微服务？
        // TODO 2.发送mq，审核服务消费？
        // TODO 3.本地服务进行审核操作？

        return Result.ok();
    }
}

